In [12]:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import fsolve
%matplotlib inline

def sigmoid(x):
    return 1. / (1 + np.exp(-x))

def df(x, w=0, theta=0):
    return w * sigmoid(x) * (1 - sigmoid(x))

def f(x, w=0, theta=0):
    return w * sigmoid(x) + theta

def g(x, w, theta):
    return f(x, w, theta) - x

1.1

We can see in the plot the fixed points. In the central point the function has a slope bigger than one, so it is not a stable point. For the other two is the opposite.


In [13]:
x = np.linspace(-5, 5, 100)
theta = -3.5
w = 8.
plt.plot(x, f(x, w, theta))
plt.plot(x, x)
plt.xlabel('x')

FP = []
for x_0 in range(-4, 4):
    temp, _, ier, _ =  fsolve(g, (x_0), args=(w, theta), full_output=True)
    if ier == 1:
        FP.append(temp)
FP = np.array(FP)
plt.plot(FP, f(FP, w, theta), '*')


Out[13]:
[<matplotlib.lines.Line2D at 0x7fe04fd80d68>]

In [58]:
for theta in np.arange(-10, 0, .05):
    x = np.arange(-10, 10, dtype='float')
    stable = []
    unstable = []
    for i in range(len(x)):
        temp, _, ier, _ =  fsolve(g, (x[i]), args=(w, theta), full_output=True)


        if ier == 1:
            x[i] = temp
            if abs(df(temp, w,theta)) < 1:
                stable.append(temp)
            else:
                unstable.append(*temp)
        else:
            x[i] = None
    plt.plot(theta * np.ones(len(stable)), stable, '.', color='green')
    plt.plot(theta * np.ones(len(unstable)), unstable, '.', color='red')



In [42]:
fsolve(g, (-1), args=(w, -10))


Out[42]:
array([-9.99963669])

In [55]:
y = np.linspace(-10, 10, 100)
for i in np.arange(-5, -3, .5):
    plt.plot(y, f(y, w, i))
plt.plot(y, y)


Out[55]:
[<matplotlib.lines.Line2D at 0x7f7ee3b19160>]

In [62]:
x, di, ler, s = fsolve(g, (-10), args=(w, -10), full_output=True)

In [63]:
x


Out[63]:
array([-9.99963669])

In [64]:
ler


Out[64]:
1

In [66]:
plt.plot(np.arange(3), [1, 2, None])


Out[66]:
[<matplotlib.lines.Line2D at 0x7f7ee3569e48>]

In [78]:
def l(x, r):
    return x * np.exp(r* (1-x))

In [88]:
y = np.linspace(-1, 4, 100)

plt.plot(y, l(y, np.exp(1)))
plt.plot(y, y)


Out[88]:
[<matplotlib.lines.Line2D at 0x7f7ee35fa710>]

In [97]:
Npre = 200
Nplot = 100
x =  np.zeros((Nplot, 1))
for r in np.arange(1, 25, .5):
    x[0] = np.random.random() * 100
    for n in range(Npre):
        x[0] = l(x[0], r)
    for n in range(Nplot - 1):
        x[n + 1] = l(x[n], r)
        
    plt.plot(r * np.ones((Nplot,  1)), x, '.')



In [22]:
#hj = set()
hj.add(100)

In [23]:
hj


Out[23]:
{1, 2, 100}

In [36]:
fsolve(g, (x[0]), args=(w, theta), full_output=True)


Out[36]:
(array([ nan]),
 {'fjac': array([[ nan]]),
  'fvec': array([ nan]),
  'nfev': 13,
  'qtf': array([ nan]),
  'r': array([ nan])},
 5,
 'The iteration is not making good progress, as measured by the \n  improvement from the last ten iterations.')

In [35]:
'nan'


Out[35]:
'nan'

In [ ]: